<?php

namespace App\Http\Controllers;

use DB;
use Request;
use Validator;

use Laravel\Lumen\Routing\Controller;

class BlessController extends BaseController
{
	/**
     * @api {get} /invitation GetTeam
     *
     * @apiVersion 0.1.0
	 *
     * @apiName GetInvitation
 	 *
     * @apiGroup invitation
	 *
     * @apiPermission app
 	 *
     *
 	 *
     * @apiDescription 获取所有喜帖
 	 *
     * @apiParam {Number} user_id 用户id.
	 *
     * @apiParam {String} search 团队名称.
	 *
     * @apiParam {Number} page 分页页码.
	 *
     *
 	 *
     * @apiSuccess {Boolean} error 是否错误.
 	 *
     * @apiSuccess {Object} result 团队数据.
 	 *
     * @apiSuccess {String} name 团队名称.
 	 *
     * @apiSuccess {Number} leader_id 创始人id.
 	 *
     * @apiSuccess {String} leader_name 创始人名称.
 	 *
     * @apiSuccess {String} is_public 是否公开.
 	 *
     * @apiSuccess {Number} set_id 通知集id.
 	 *
     * @apiSuccess {Number} calendar_id 日历id.
 	 *
     * @apiSuccess {String} easemob_id 环信群组id.
 	 *
     * @apiSuccess {String} description 团队描述.
 	 *
     * @apiSuccess {String} icon 团队图标.
 	 *
     * @apiSuccess {DateTime} created_at 创建时间.
 	 *
     * @apiSuccess {String} message 提示消息.
 	 *
 	 *
     * @apiError paramError 参数错误.
 	 *
     * @apiSampleRequest http://192.168.1.120:82/api/team
 	 *
     */

	public function index()
	{

		$result = DB::table("bless")
          ->join("invitation","invitation.id","=","bless.invitation_id")
          ->select( "bless.invitation_id",
                    "bless.name",
                    "bless.phone",
                    "bless.content",
                    "bless.create_at",
                    "invitation.boy_name",
                    "invitation.girl_name")
          ->where("invitation.user_id",$_SESSION["ID"])
          ->where("invitation.is_delete",false)
          ->get();
          if(empty($result)) {
               return $this->jsonResponse(true,"","该喜帖还没人恢复参加哦");
          }else{
                return $this->jsonResponse(false,$result);
          }
	}

	public function show($id)
	{
         

	}

	public function store($invitationId)
	{
		$input = Request::all();
       // 参数检验
        $validator = Validator::make($input,
            [
                'name'     => 'required|string',
                 'phone'   => 'required|string|size:11',
                'content'  => 'required|string',
            ]
        );
        if($validator->fails()){
            return $this::jsonResponse(true,$validator->messages(),"paramError");
        }

        $exit = DB::table("invitation")
        ->where("id",$invitationId)
        ->get();
        if (!empty($exit)) {
            if(isset($input["number"])){
                $attend = [
                    "invitation_id"=>$invitationId,
                    "name"=>$input["name"],
                    "phone"  =>$input["phone"],
                    "number"  =>$input["number"]
               ];

               $attend_result = DB::table("attend")
               ->insertGetId($attend);
            }
            $bless = [
                "invitation_id"=>$invitationId,
                "name"=>$input["name"],
                "phone"  =>$input["phone"],
                "content"  =>$input["content"]
            ];
            $bless_result = DB::table("bless")
            ->insertGetId($bless);

            if ($bless_result) {
               return $this->jsonResponse(false,"插入成功");
            }else{
                return $this->jsonResponse(true,"插入失败！");
            }
        }else{
            return $this->jsonResponse(true,'',"喜帖不存在");
        }
	}
	public function update($id)
	{
		# code...
	}

	public function destory($id)
	{
		# code...
	}
}